package com.wjb.xietong.ota;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

import com.igexin.sdk.PushManager;
import com.wjb.xietong.R;
import com.wjb.xietong.component.ActManager;
import com.wjb.xietong.component.AppGlobal;
import com.wjb.xietong.ota.login.OTALogin;
import com.wjb.xietong.util.LogD;
import com.wjb.xietong.util.PreferencesService;

public class OTASplash extends Activity {

	private AppGlobal app_;

	// SDK参数，会自动从Manifest文件中读取，第三方无需修改下列变量，请修改AndroidManifest.xml文件中相应的meta-data信息。
	// 修改方式参见个推SDK文档
	private String appkey = "";
	private String appsecret = "";
	private String appid = "";
	private int retryTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		app_ = (AppGlobal) getApplication();
		app_.setActManager(ActManager.Instance());
		DisplayMetrics display = this.getResources().getDisplayMetrics();
		app_.setDisplayMetrics(display);

		app_.getActManager().pushActivity(this);

		// 初始化个推 SDK
		initGetuiSDK();

		setContentLayout();

		TelephonyManager tm = (TelephonyManager) this
				.getSystemService(TELEPHONY_SERVICE);
		AppGlobal.getInstance().setImei(tm.getDeviceId());

		AppGlobal.getInstance().setAppPath(
				getApplicationContext().getFilesDir().getAbsolutePath());

		try {
			// 获取当前程序版本
			PackageInfo packageInfo = getApplicationContext()
					.getPackageManager().getPackageInfo(getPackageName(), 0);
			AppGlobal.getInstance().setLocalAppVersion(packageInfo.versionName);
		} catch (NameNotFoundException e) {
			LogD.output("Exception:" + e.getMessage());
		}

		PreferencesService preferences = new PreferencesService(AppGlobal
				.getInstance().getBaseContext());
		String login = preferences.getUserLogin();
		if ((login == null) || (login != null && login.equalsIgnoreCase("no"))){
			gotoShowPage();
		}
		else{
			gotoNextScreen();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void gotoShowPage(){
		Intent intent = new Intent(OTASplash.this, OTAStartupShow.class);
		startActivity(intent);
	}

	private void gotoNextScreen() {
		Intent intent = new Intent(OTASplash.this, OTALogin.class);
		startActivity(intent);
		app_.getActManager().popActivity();
	}

	private void setContentLayout() {
		setContentView(R.layout.dialog_help);

	}

	private void initGetuiSDK() {
		// 从AndroidManifest.xml的meta-data中读取SDK配置信息
		String packageName = getApplicationContext().getPackageName();
		ApplicationInfo appInfo;
		try {
			appInfo = getPackageManager().getApplicationInfo(packageName,
					PackageManager.GET_META_DATA);
			if (appInfo.metaData != null) {

				appid = appInfo.metaData.getString("PUSH_APPID");
				appsecret = appInfo.metaData.getString("PUSH_APPSECRET");
				appkey = (appInfo.metaData.get("PUSH_APPKEY") != null) ? appInfo.metaData
						.get("PUSH_APPKEY").toString() : null;
			}

		} catch (NameNotFoundException e) {
			LogD.output("Exception:" + e.getMessage());
		}

		// 初始化个推SDK
		PushManager.getInstance().initialize(this.getApplicationContext());
	}

}
